package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.GoodsType;
import com.xinhe.web.entity.QGoods;
import com.xinhe.web.entity.QGoodsPrice;
import com.xinhe.web.reposity.GoodsRepository;
import com.xinhe.web.reposity.GoodsTypeRepository;
import com.xinhe.web.response.GoodsTypeView;
import com.xinhe.web.response.GoodsView;
import com.xinhe.web.util.Snowflake;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import java.util.List;

@Service

public class GoodsTypeService {
    @Autowired
    private GoodsTypeRepository goodsTypeRepository;
    @Autowired
    private GoodsRepository goodsRepository;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;

    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    public Result<List<GoodsTypeView>> pageGoodsType() {
        List<GoodsTypeView> views = this.goodsTypeRepository.pageGoodsType();
        if (CollectionUtils.isNotEmpty(views)) {
            for (GoodsTypeView view : views) {
                QGoods qGoods = QGoods.goods;
                QGoodsPrice qGoodsPrice = QGoodsPrice.goodsPrice;
                BooleanBuilder builder = new BooleanBuilder();
                builder.and(qGoods.fGoodsTypeId.eq(view.getId()));
                //builder.and(qGoods.removed.eq(0));
                List<GoodsView> goodsViews = queryFactory.select(
                        Projections.bean(
                                GoodsView.class,
                                qGoods.sellerUserId,
                                qGoods.fGoodsTypeId,
                                qGoods.id,
                                qGoods.goodsZoneType,
                                qGoods.fGoodsZoneTypeId,
                                qGoods.name,
                                qGoods.iconPath,
                                qGoods.dis,
                                qGoods.price,
                                qGoods.sendAddress,
                                qGoods.freightFee,
                                qGoodsPrice.originalPrice,
                                qGoodsPrice.unit
                        )
                ).from(qGoods)
                        .leftJoin(qGoodsPrice).on(qGoods.priceId.eq(qGoodsPrice.id))
                        .where(builder).fetch();

                if (CollectionUtils.isNotEmpty(goodsViews)) {
                    view.setGoodsViews(goodsViews);
                }
            }
        }
        return Result.ok(views);
    }

    public Result<GoodsType> findById(Long goodsTypeId) {
        return Result.ok(goodsTypeRepository.findById(goodsTypeId).get());
    }

    public Result delete(Long goodsTypeId) {
        goodsTypeRepository.deleteById(goodsTypeId);
        return Result.ok();
    }

    public Result saveModel(Long id, String name, String iconPath, Integer seq, String remark) {
        GoodsType goodsType = new GoodsType();
        goodsType.setId(Snowflake.generateId());
        goodsType.setIconPath(iconPath);
        goodsType.setName(name);
        goodsType.setSeq(seq);
        goodsTypeRepository.save(goodsType);
        return Result.ok();
    }
}
